package com.example.guiadigital4;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONObject;

public class parsee {
	
	public void parsear(){
		
		String stringUrl = "http://maps.googleapis.com/maps/api/directions/json?origin=Aconcagua+Montevideo&destination=Parque+Rodo+Montevideo&sensor=false";
		String response="";
		try{
	      URL url = new URL(stringUrl);
	    
	      /*HttpURLConnection httpconn = (HttpURLConnection)url.openConnection();
	      if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK)
	      {
	          BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream()),8192);
	          String strLine = null;
	          while ((strLine = input.readLine()) != null)
	          {
	              //response.append(strLine);
	        	  response+=strLine;
	          }
	          input.close();
	      }
	      String jsonOutput = response.toString();*/
	      
	      //String result = actividad.getResources().getString(R.string.error);
			HttpURLConnection con =null;
						
	   //   String q= URLEncoder.encode(original,"UTF-8");
		//	URL url= new URL("http://ajax.googleapis.com/ajax/services/language/translate" +
		//			"?v=1.0" + "&q="+ q+ "&langpair=" + desde+ "%7C" +hacia);
			con=(HttpURLConnection) url.openConnection();
			con.setReadTimeout(10000);
			con.setConnectTimeout(15000);
			con.setRequestMethod("GET");
			con.setDoInput(true);
			con.connect();
			
			BufferedReader reader= new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
			String payload= reader.readLine();
			reader.close();
			
			//parsea el resultado
			JSONObject jsonObject= new JSONObject(payload);
			//result=jsonObject.getJSONObject("responseData").getString("TranslatedText").replace("&#39;","'").replace("&amp;","&");
	      
				
	//JSONObject jsonObject = new JSONObject(jsonOutput);

	// routesArray contains ALL routes
	JSONArray routesArray = jsonObject.getJSONArray("routes");
	// Grab the first route
	JSONObject route = routesArray.getJSONObject(0);
	// Take all legs from the route
	JSONArray legs = route.getJSONArray("legs");
	// Grab first leg
	JSONObject leg = legs.getJSONObject(0);

	JSONObject durationObject = leg.getJSONObject("duration");
	String duration = durationObject.getString("text");
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
	}
}

